package com.tingyuge.base.thread.wang.chapter9;

import java.util.Arrays;

/**
 * Created by xiaodao
 * date: 2019/11/29
 */
public class ThreadGroupCreate {

    public static void main(String[] args) {
        //use the name
        ThreadGroup tg1 = new ThreadGroup("tg1");
        Thread t1 = new Thread(tg1, "t1") {
            @Override
            public void run() {
//                while (true) {
                    try {
                        System.out.println(getThreadGroup().getName());
                        System.out.println(getThreadGroup().getParent());
                        System.out.println(getThreadGroup().getParent().activeCount());
                        Thread.sleep(1000);
                        System.out.println("----");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
//                    }
                }
            }
        };
        t1.start();
        ThreadGroup tg2 = new ThreadGroup(tg1, "tg2");
        Thread t2 =  new Thread(tg2,"t2"){
            @Override
            public void run() {
                System.out.println(tg1.getName());
                Thread[] threads =  new Thread[tg1.activeCount()];
                tg1.enumerate(threads);
                Arrays.asList(threads).stream().forEach(System.out::println);
            }
        };
        t2.start();
        /*       System.out.println(t2.getParent().getName());*/
//        System.out.println(t1.getThreadGroup().getName());


        //use the parent and group name
    }
}
